package com.ruoyi.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.api.annotation.Login;
import com.ruoyi.business.app.vo.AppSurplusTimesVo;
import com.ruoyi.business.domain.HcxMerchantOrder;
import com.ruoyi.business.domain.HcxMerchantProduct;
import com.ruoyi.business.service.IHcxMerchantOrderService;
import com.ruoyi.business.service.IHcxMerchantProductService;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;


@Api(tags = "商家对订单进行核销管理")
@AllArgsConstructor
@RestController
@RequestMapping("/api/order")
public class CheckController extends BaseController {
    private final IHcxMerchantOrderService merchantOrderService;


    private final IHcxMerchantProductService merchantProductService;

    @ApiOperation("扫一扫结果页面")
    @GetMapping("/scanResult")
    @Login
    public AjaxResult<AppSurplusTimesVo> scanResult(@ApiParam("订单编号") @RequestParam String orderNum) {
        // 查询订单信息
        HcxMerchantOrder hcxMerchantOrder = merchantOrderService.getOne(new LambdaQueryWrapper<HcxMerchantOrder>().eq(HcxMerchantOrder::getOrderNum, orderNum));

        AppSurplusTimesVo appSurplusTimesVo=new AppSurplusTimesVo();

        // 如果是商品
        if(Constants.ZERO.equals(hcxMerchantOrder.getOrderType())){

            HcxMerchantProduct hcxMerchantProduct = merchantProductService.getById(hcxMerchantOrder.getProductId());
            appSurplusTimesVo.setProId(hcxMerchantProduct.getId());
            appSurplusTimesVo.setPicUrl(hcxMerchantProduct.getPicUrl());
            appSurplusTimesVo.setProName(hcxMerchantProduct.getName());
            appSurplusTimesVo.setProPrice(hcxMerchantProduct.getProPrice());
            appSurplusTimesVo.setSummary(hcxMerchantProduct.getSummary());
        }

        appSurplusTimesVo.setSurplusTimes(hcxMerchantOrder.getSurplusTimes());
        appSurplusTimesVo.setRealName(hcxMerchantOrder.getRealName());
        appSurplusTimesVo.setOrderType(hcxMerchantOrder.getOrderType());

        return AjaxResult.success(appSurplusTimesVo);
    }


    @ApiOperation("商家对某个订单进行核销处理")
    @PostMapping("/check")
    @Login
    @RepeatSubmit
    public AjaxResult check(@ApiParam("订单编号") @RequestParam String orderNum) {
        boolean evaluate = merchantOrderService.checkOrder(orderNum);
        return toAjax(evaluate);
    }
}
